package ru.spbsu.electromenu.serialization.json;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.spbsu.electromenu.model.Alert;
import ru.spbsu.electromenu.model.Order;
import ru.spbsu.electromenu.model.Table;
import ru.spbsu.electromenu.serialization.DeserializationException;
import ru.spbsu.electromenu.serialization.Deserializer;

import java.util.Date;
import java.util.HashMap;

/**
 * Date: 3/22/11
 * Time: 11:37 PM
 *
 * @author Artem Titov
 */
public class JSONDeserializer implements Deserializer {
    private static final Logger logger = LoggerFactory.getLogger(JSONDeserializer.class);

    @Override
    public Table deserializeTable(final String source) throws DeserializationException {
        if (source == null) {
            throw new DeserializationException("Source is null");
        }
        try {
            logger.debug("Incoming table source: {}", source);
            final JSONObject json = new JSONObject(source);
            final Table table = new Table();
            table.setId(json.getInt("id"));
            return table;
        } catch (JSONException e) {
            throw new DeserializationException(e);
        }
    }

    @Override
    public Alert deserializeAlert(final String source) throws DeserializationException {
        if (source == null) {
            throw new DeserializationException("Source is null");
        }
        try {
            logger.debug("Incoming alert source: {}", source);
            final JSONObject json = new JSONObject(source);
            final Alert alert = new Alert();
            alert.setSessionId(json.getInt("session-id"));
            alert.setAlertMessage(json.getString("alert-message"));
            alert.setTime(new Date(json.getLong("time") * 1000));
            alert.setActive(true);
            return alert;
        } catch (JSONException e) {
            throw new DeserializationException(e);
        }
    }

    @Override
    public Order deserializeOrder(final String source) throws DeserializationException {
        if (source == null) {
            throw new DeserializationException("Source is null");
        }
        try {
            logger.debug("Incoming order source: {}", source);
            final JSONObject json = new JSONObject(source);
            final Order order = new Order();
            order.setId(json.getInt("id"));
            order.setSessionId(json.getInt("session-id"));
            order.setTime(new Date(json.getLong("time") * 1000));
            final HashMap<Integer, Integer> orderedDishes = new HashMap<Integer, Integer>();
            for (int i = 0; i < json.getJSONArray("ordered-dishes").length(); i++) {
                final int dishId = json.getJSONArray("ordered-dishes").getInt(i);
                final int currentAmount = (orderedDishes.containsKey(dishId)) ? orderedDishes.get(dishId) : 0;
                orderedDishes.put(dishId, currentAmount + 1);
            }
            order.setOrderedDishesIds(orderedDishes);
            return order;
        } catch (JSONException e) {
            throw new DeserializationException(e);
        }
    }
}
